package com.jbinfo.mongoot.controller;

import com.jbinfo.mongoot.operation.archive.Archive;
import com.jbinfo.mongoot.operation.assessment.Evaluate;
import com.jbinfo.mongoot.operation.assessment.EvaluateRepository;
import com.jbinfo.mongoot.operation.plan.ServicePlan;
import com.jbinfo.mongoot.operation.plan.ServicePlanRepository;
import com.jbinfo.mongoot.util.XiMethodUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务评估
 * Created by wizard on 2017/4/5.
 */
@RestController
@RequestMapping("/evaluate")
@Api("fuzhou-evaluate类API")
public class EvaluateController {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private EvaluateRepository evaluateRepository;

    @ApiOperation("通过post请求添加评估信息")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Map<String, Object> add(@ModelAttribute Evaluate evaluate) {
        Map<String, Object> map = new HashMap() ;
        String description="";

        if("".equals(description)){ //非空
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            evaluate.setCreateDate(simpleDateFormat.format(new Date()));
            evaluateRepository.save(evaluate);
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }

    @ApiOperation("通过oldid修改老人服务评估信息")
    @RequestMapping(value = "/updateEvaluateByOldid", method = RequestMethod.POST)
    public Map<String, Object> updateEvaluateByCardid(@ModelAttribute Evaluate evaluate) throws ParseException {
        Map<String, Object> map = new HashMap() ;
        String description="";
        //验证参数是否为空

        if("".equals(description)){ //非空
            DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(evaluate.getClass()));
            //唯一字段
            DBObject query = new BasicDBObject();
            query.put("oldid", evaluate.getOldid());
            //找到要修改的对象
            DBObject findOne = collection.findOne(query);
            //修改时间变更
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss");
            evaluate.setUpdateDate(simpleDateFormat.format(new Date()));
            DBObject update = XiMethodUtil.method(evaluate,findOne);//要找其他字段原来的值，不然update会给你自动修改其他的值为null
            collection.update(query, update, false, true);//有的话修改，没有的话进行添加！第三个参数为true。
            map.put("description", "");
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }

    @ApiOperation("按老人oldid查询服务评估信息")
    @RequestMapping(value = "/findEvalueteByOldid", method = RequestMethod.GET)
    public Map<String, Object> findEvalueteByOldid(@RequestParam("oldid") String oldid) {
        Map<String, Object> map = new HashMap() ;
        String description="";
        //验证参数是否为空

        if("".equals(description)){ //非空
            Query query = new Query();
            Criteria criatira = new Criteria();
            criatira.andOperator(Criteria.where("oldid").is(oldid));
            Evaluate evaluate = mongoTemplate.findOne(query.addCriteria(criatira),Evaluate.class);
            map.put("result", evaluate);
            map.put("status", 1);
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }






}
